# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: jhkim

from absl import app, flags

import sys
import json
import pandas

from coin.base.config import Config
from coin.exchange.okex_v3.kr_rest.native_private_client import (OkexNativePrivateClient)
from coin.exchange.okex_futures.kr_rest.native_private_client import (OkexFuturesNativePrivateClient
                                                                     )
from coin.exchange.okex_futures.kr_rest.futures_product import OkexFuturesProduct
from coin.exchange.okex.kr_rest.product import OkexProduct

from coin.exchange.okex_futures_swap.kr_rest.native_private_client import (
    OkexSwapNativePrivateClient, OkexFuturesOrderStatus, OkexOrderStatus)


def print_json(update):
  print(json.dumps(update.msg, indent=2))


def main(_):
  assert flags.FLAGS.og_config_name is not None
  og_config = Config.from_config_filename(flags.FLAGS.og_config_name)
  print(sys.argv)
  if (flags.FLAGS.symbol.find("USDT") >= 0):
    symbol = OkexProduct.FromStr(flags.FLAGS.symbol)
    client = OkexNativePrivateClient(key_file=og_config.key_file)
    recent_orders = client.query_order_list(
        status=[OkexOrderStatus.FILLED],
        instrument_id=symbol.native_symbol_v3,
        limit=5,
    )
    order_id = recent_orders.msg[-1]['order_id']
    orders = client.query_transaction_details(order_id=order_id,
                                              instrument_id=symbol.native_symbol_v3,
                                              limit=20)
    df = pandas.DataFrame(orders.msg)
    print(df)
  elif (flags.FLAGS.symbol.find("PERPETUAL") >= 0):
    symbol = OkexFuturesProduct.FromStr(flags.FLAGS.symbol)
    client = OkexSwapNativePrivateClient(key_file=og_config.key_file)
    orders = client.query_order_list(status=OkexFuturesOrderStatus.FILLED,
                                     instrument_id=symbol.native_symbol_v3,
                                     limit=5)
    df = pandas.DataFrame(orders.msg['order_info'])
    df['fee_rate'] = df['price_avg'].astype(float) * df['fee'].astype(
        float) / df['filled_qty'].astype(float) / df['contract_val'].astype(float)
    print(df)
  else:
    symbol = OkexFuturesProduct.FromStr(flags.FLAGS.symbol)
    client = OkexFuturesNativePrivateClient(og_config.key_file)
    orders = client.query_POST_futures_order_info(symbol=symbol.order_symbol,
                                                  contract_type=symbol.contract_type,
                                                  status='filled',
                                                  page_length=5).msg['orders']
    df = pandas.DataFrame(orders)
    df['fee_rate'] = df['price_avg'] * df['fee'] / df['deal_amount'] / df['unit_amount']
    print(df)
  # import pdb; pdb.set_trace()


'''
./pyrunner coin/support/exchange_status/app/okex_futures_fee_checker.py --og_config_name=trade/xunke00/OKEx/trade_futures2_key.json --symbol=ETH-USD.QUARTER
./pyrunner coin/support/exchange_status/app/okex_futures_fee_checker.py --og_config_name=trade/xunke01/OKEx/trade_futures2_key.json --symbol=ETH-USD.THIS_WEEK
./pyrunner coin/support/exchange_status/app/okex_futures_fee_checker.py --og_config_name=trade/xunke02/OKEx/trade_futures2_key.json --symbol=ETH-USD.QUARTER
./pyrunner coin/support/exchange_status/app/okex_futures_fee_checker.py --og_config_name=/home/jhkim/workspace/coin_key_high/trade/xunke05/OKEx/trade_v3_key.json --symbol=ETH-USD.PERPETUAL
'''

if __name__ == '__main__':
  flags.DEFINE_string('og_config_name', None, 'og_config_name')

  flags.DEFINE_string('symbol', None, '')
  pandas.set_option('display.width', 1000)
  pandas.set_option('display.max_rows', None)
  pandas.set_option('display.max_columns', None)
  app.run(main)
